import {
  AppFullReg,
  AppReg,
  checkAppPath,
  checkControlPath,
  checkViewPath,
  ControlFullReg,
  ControlReg,
  ViewFullReg,
  ViewReg
} from "../util";

export interface IModelApp {
  /**
   * @description 类型
   * @type {('APP' | 'VIEW' | 'CONTROL' | string)}
   * @memberof IModelApp
   */
  type: 'APP' | 'VIEW' | 'CONTROL' | string;

  /**
   * 代码名
   *
   * @type {string}
   * @memberof IModelApp
   */
  codeName: string;

  /**
   * @description 路径
   * @type {string}
   * @memberof IModelApp
   */
  path: string;

  /**
   * @description 使用本地服务
   * @type {boolean}
   * @memberof IModelApp
   */
  useLocalService: boolean;

  /**
   * @description 远端服务接口
   * @type {string}
   * @memberof IModelApp
   */
  host: string;

  /**
   * @description 运行端口
   * @type {number}
   * @memberof IModelApp
   */
  port?: number;
}

/**
 * @description 模型应用实例对象
 * @export
 * @class ModelApp
 * @implements {IModelApp}
 */
export class ModelApp implements IModelApp {

  /**
   * @description 基础路径
   * @type {string}
   * @memberof ModelApp
   */
  public basePath: string = '';

  /**
   * 应用代码名称
   *
   * @type {string}
   * @memberof ModelApp
   */
  public codeName: string = '';

  /**
   * @description 应用路径
   * @private
   * @type {string}
   * @memberof ModelApp
   */
  public appPath: string = '';

  /**
   * @description 类型
   * @type {('APP' | 'VIEW' | 'CONTROL' | string)}
   * @memberof ModelApp
   */
  public type: 'APP' | 'VIEW' | 'CONTROL' | string = 'APP';

  /**
   * @description 路径
   * @type {string}
   * @memberof ModelApp
   */
  public path: string = '';

  /**
   * @description 使用本地服务
   * @type {boolean}
   * @memberof ModelApp
   */
  public useLocalService: boolean = true;

  /**
   * @description 服务地址
   * @type {string}
   * @memberof ModelApp
   */
  public host: string = '';

  /**
   * @description 静态资源映射
   *
   * @type {string}
   * @memberof ModelApp
   */
  public staticResource: string = '';

  /**
   * Creates an instance of ModelApp.
   * @param {*} opts
   * @memberof ModelApp
   */
  public constructor(opts: IModelApp) {
    this.init(opts);
  }

  /**
   * @description 设置类型
   * @param {string} type
   * @memberof ModelApp
   */
  setType(type: string) {
    this.type = type;
  }

  /**
   * @description 设置路径
   * @param {string} path
   * @memberof ModelApp
   */
  setPath(path: string) {
    switch (this.type) {
      case 'APP':
        this.path = this.handleAppPath(path);
        break;
      case 'VIEW':
        this.path = this.handleViewPath(path);
        break;
      case 'CONTROL':
        this.path = this.handleControlPath(path);
        break;
    }
  }

  /**
   * @description 初始化配置
   * @param {IModelApp} options
   * @memberof ModelApp
   */
  private init(options: IModelApp) {
    this.codeName = options.codeName;
    this.type = options.type;
    this.path = options.path;
    this.useLocalService = options.useLocalService === false ? false : true;
    this.host = options.host;
    this.staticResource = '/app/index.html';
    this.initPath();
  }

  /**
   * @description 初始化基础路径
   * @private
   * @memberof ModelApp
   */
  private initPath() {
    try {
      const reg = /(PSSYSAPPS[\/\\]{1,2})([a-zA-Z0-9_]*)/g;
      if (this.path.search(reg) != -1) {
        this.appPath = this.path.match(reg)[0].replace(reg, "PSSYSAPPS/$2");
      }
      this.basePath = this.path.slice(0, this.path.search(reg));
    } catch (error) {
    }
  }

  /**
   * @description 获取真实路径
   * @param {string} path
   * @return {*} 
   * @memberof ModelApp
   */
  public getPath(path: string) {
    let realPath: string = '';
    if (path.indexOf('PSSYSAPPS') !== -1) {
      realPath = path;
    } else {
      realPath = this.basePath + this.appPath + path;
    }
    return realPath;
  }

  /**
   * @description 处理应用路径
   * @private
   * @param {string} path 应用路径
   * @return {*} 
   * @memberof ModelApp
   */
  private handleAppPath(path: string) {
    if (path == '' || path == undefined) {
      return '/' + this.appPath;
    }
    if (checkAppPath(path)) {
      if (path.search(AppFullReg) != -1) {
        path.replace(AppFullReg, (arg: string) => {
          this.appPath = arg;
          return arg;
        });
        return '/' + this.appPath;
      }
      if (path.search(AppReg) != -1) {
        //  设置app路径
        path.replace(AppReg, (arg: string) => {
          this.appPath = 'PSSYSAPPS/' + arg;
          return arg;
        });
        return '/' + this.appPath;
      }
    }
  }

  /**
   * @description 处理视图路径
   * @private
   * @param {string} path 视图路径
   * @return {*} 
   * @memberof ModelApp
   */
  private handleViewPath(path: string) {
    if (checkViewPath(path)) {
      if (path.search(ViewFullReg) != -1) {
        return this.basePath + this.appPath + '/' + path.slice(path.search(ViewFullReg));
      }
      if (path.search(ViewReg) != -1) {
        return this.basePath + this.appPath + '/PSAPPDEVIEWS/' + path.slice(path.search(ViewReg));
      }
      return path;
    }
  }

  /**
   * @description 处理部件路径
   * @private
   * @param {string} path 部件路径
   * @return {*} 
   * @memberof ModelApp
   */
  private handleControlPath(path: string) {
    if (checkControlPath(path)) {
      if (path.search(ControlFullReg) != -1) {
        return this.basePath + this.appPath + '/' + path.slice(path.search(ControlFullReg));
      }
      if (path.search(ControlReg) != -1) {
        return this.basePath + this.appPath + '/PSAPPDATAENTITIES/' + path.slice(path.search(ControlReg));
      }
      return path;
    }
  }

}